// package-star/pages/brand-withdraw-card/index.js
import { ManageTargetInfoUpdateMixins } from '../../../behaviors/page';
import { WXPAYMENT_ACCOUNT_TYPE_VALUE, WXPAYMENT_ACCOUNT_TYPE, WXPAYMENT_ORGANIZATION_TYPE_VALUE } from '../../../constants/payment';
import { navBack, tipToast, showLoading, hideLoading, tipSuccess } from '../../../common/env';
import { areaCode2Str } from '../../../common/utils';
import { getAreaList } from '../../../api/common';
import { getWxpayApplyDetail, editBankCard } from '../../../api/brd';

Page({
    mixins: [ManageTargetInfoUpdateMixins],
    data: {
        WXPAYMENT_ORGANIZATION_TYPE_VALUE,
        WXPAYMENT_ACCOUNT_TYPE_VALUE,
        WXPAYMENT_ACCOUNT_TYPE,
        organizationType: '',
        bankCard: {},
        areaPickerPopup: {
            show: false,
            areaStr: '',
            areaList: {},
        },
        bankPickerPopup: {
            show: false,
        },
    },
    onLoad(options) {
        this._handleGetArea();
        this._handleGetDetail();
    },
    async _handleGetArea() {
        const areaList = await getAreaList();
        this.setData({
            'areaPickerPopup.areaStr': '',
            'areaPickerPopup.areaList': areaList,
        })
    },
    async _handleGetDetail() {
        showLoading('加载中...');
        const applyInfo = await getWxpayApplyDetail();
        const { organizationType } = applyInfo.applyFormData;
        const { bankCard } = this.data;
        if (organizationType === WXPAYMENT_ORGANIZATION_TYPE_VALUE.micro) {
            bankCard.accountType = WXPAYMENT_ACCOUNT_TYPE_VALUE.ACCOUNT_TYPE_PRIVATE;
        } else if (organizationType === WXPAYMENT_ORGANIZATION_TYPE_VALUE.company) {
            bankCard.accountType = WXPAYMENT_ACCOUNT_TYPE_VALUE.ACCOUNT_TYPE_BUSINESS;
        }
        this.setData({
            organizationType,
            bankCard,
        });
        hideLoading();
    },
    _handleGetCodeStr() {
        const { bankAddressCode } = this.data.bankCard;
        const { areaList } = this.data.areaPickerPopup;
        if (bankAddressCode) {
            this.setData({
                'areaPickerPopup.areaStr': areaCode2Str(areaList, bankAddressCode),
            });
        }
    },
    handleChange(e) {
        const { bankCard } = this.data;
        const { type } = e.currentTarget.dataset;
        const value = e.detail;
        bankCard[type] = value;
        this.setData({ bankCard });
    },
    handleAccountType(e) {
        const { bankCard } = this.data;
        const { value } = e.currentTarget.dataset;
        bankCard.accountType = value;
        this.setData({ bankCard });
    },
    handleSaveArea(e) {
        const { bankCard } = this.data;
        const { values } = e.detail;
        const value = values[2] || values[1] || values[0];
        bankCard.bankAddressCode = value.code;
        this.setData({ bankCard });
        this._handleGetCodeStr();
    },
    handleSaveBank(e) {
        const { bankCard } = this.data;
        const { value } = e.detail;
        bankCard.accountBank = value.name;
        this.setData({ bankCard });
    },
    handleShowBankPickerPopup() {
        this.setData({
            'bankPickerPopup.show': true,
        });
    },
    handleShowAreaPickerPopup() {
        this.setData({
            'areaPickerPopup.show': true,
        });
    },
    async handleSave() {
        const { bankCard } = this.data;
        const { accountType, accountBank, accountNumber, bankName, bankAddressCode } = bankCard;
        if (!accountType) {
            tipToast('请选择账户类型');
            return;
        }
        if (!accountNumber) {
            tipToast('请输入银行账号');
            return;
        }
        if (!accountBank) {
            tipToast('请选择开户银行');
            return;
        }
        // if (!bankName) {
        //     tipToast('请输入银行全称');
        //     return;
        // }
        if (!bankAddressCode) {
            tipToast('请选择开户地');
            return;
        }
        showLoading('保存中...');
        const flag = await editBankCard(bankCard);
        if (flag) {
            hideLoading();
            tipSuccess('保存成功');
            navBack();
        }
    },
})
